import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pleasure_video/config/application.dart';
import 'package:flutter_pleasure_video/constant/app_icon.dart';
import 'package:flutter_pleasure_video/data/database/yck_rule_helper.dart';
import 'package:flutter_pleasure_video/data/model/yck_rule_model.dart';
import 'package:flutter_pleasure_video/util/screen_util.dart';
import 'package:flutter_pleasure_video/widget/common/app_icon_button.dart';
import 'package:flutter_pleasure_video/widget/common/buttom_common.dart';
import 'package:get/get.dart';
import 'package:flutter_pleasure_video/util/extensions/string_extensions.dart';


class ImportSourceDialog extends StatelessWidget {

  final Function()? onExitListener;
  final List<YckRuleModel> modelList;
  final ModelCheckboxController _controller=ModelCheckboxController();

  ImportSourceDialog(this.modelList, {Key? key, this.onExitListener}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    return GetBuilder<ModelCheckboxController>(
      init: _controller,
      initState: (_) {
        _controller.init(modelList);
      },
      builder: (ctl){
        return getBody(context);
      },
    );
  }


  Widget getBody(BuildContext context){
    return Container(
      child: UnconstrainedBox(
        child: Container(
          width: ScreenUtil.instance.width-80,
          child: Column(
            children: [
              SizedBox(height: 12),
              _buildTopUi(),
              _sourceList(context),
              _getBottomUi(context),
              SizedBox(height: 6),
            ],
          ),
        ),
      ),
    );
  }


  Widget _buildTopUi() {
    return Container(
      child: Row(
        children: [
          SizedBox(width: 16),
          Text("导入资源"),
          Spacer(),
          TextButtonCommon("自定义分组名",onTap: ()=>"点击了自定义分组名".print),
          SizedBox(width: 6),
          AppIconButton(iconSize: 18, svgName: AppIcon.more_feed, buttonHeight: 32, buttonWidth: 34),
        ],
      ),
    );
  }

  Widget _sourceList(BuildContext context) {
    return Container(
      constraints: BoxConstraints(
        minHeight: 30,
        maxHeight: Get.height-48*2-100,
      ),
      child: ListView.builder(
        shrinkWrap: true,
        itemBuilder: (context,index){
          return _sourceListItem(modelList[index],index);
        },
        itemCount: modelList.length,
      ),
    );
  }


  Widget _sourceListItem(YckRuleModel model,int index){
    return GestureDetector(
      onTap: (){
        _controller.setChange(model.id, (!_controller.getMap(model.id)));
      },
      child: Container(
        child: Row(
          children: [
            GetBuilder<ModelCheckboxController>(
              id: "changeController",
              builder: (ctl) => Checkbox(
                value: ctl.getMap(model.id),
                onChanged: (value){
                  ctl.setChange(model.id, value);
                },
              ),
            ),
            Text(model.title),
            Spacer(),
            Text("新资源"),
            SizedBox(width: 16),
          ],
        ),
      ),
    );
  }

  Widget _getBottomUi(BuildContext context){
    return Container(
      child: Row(
        children: [
          SizedBox(width: 10),
          GetBuilder<ModelCheckboxController>(
            init: _controller,
            builder: (ctl)=>TextButtonCommon(
              "全选(${ctl.selectCount}/${modelList.length})",
              onTap: ()=>"全选".print,
              padding:EdgeInsets.all(6),
            ),
          ),
          Spacer(),
          TextButtonCommon(
            "取消",
            onTap: ()=> Navigator.pop(context),
            padding:EdgeInsets.all(6),
          ),
          SizedBox(width: 16),
          TextButtonCommon(
            "确定",
            onTap: ()=>_sureBtnOnClickListener(context),
            padding:EdgeInsets.all(6),
          ),
          SizedBox(width: 10),
        ],
      ),
    );
  }


  //确定按钮
  _sureBtnOnClickListener(BuildContext context)async{
    for(YckRuleModel model in modelList){
      if(_controller.getMap(model.id)){
        Application.yckRuleModelMap[model.id??""]=model;
        await YckRuleHelper.init().update(model);
      }
    }
    if(onExitListener!=null){
      onExitListener!();
    }
    Navigator.pop(context);
  }

}


class ModelCheckboxController extends GetxController{
  Map<String,bool> _modelCheckboxMap=Map();
  var selectCount=0;

  init(List<YckRuleModel>? modelList){
    if(modelList!=null){
      "init:modelList:${modelList.length}".print;
      for (var value in modelList) {
        _modelCheckboxMap[value.id??""]=true;
      }
      selectCount=modelList.length;
    }
  }

  setChange(String? modelId,bool? isChange){
    if(modelId==null||isChange==null){
      return;
    }
    _modelCheckboxMap[modelId]=isChange;
    isChange?selectCount++:selectCount--;
    update(["changeController"]);
  }

  getMap(String? modelId){
    if(modelId==null){
      return false;
    }
    return _modelCheckboxMap[modelId]??false;
  }

  @override
  void onInit() {
    super.onInit();
    "onInit".print;
  }

  @override
  void onReady() {
    super.onReady();
    "onReady".print;
  }

  @override
  void onClose() {
    super.onClose();
    _modelCheckboxMap.clear();
  }

}
